package com.example.opengldemo.buffer;

import android.opengl.GLES30;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;


public class TextureRect 
{
    
    
    String mVertexShader;
    String mFragmentShader;
    ShaderHandle shaderHandle ;
    ShaderBuffer shaderBuffer ;

    public TextureRect(MySurfaceView mv,float scale,float a,float b)
    {
        shaderBuffer = new ShaderBuffer() ;
        initVertexData(scale,a,b);
    	initShader(mv);

    }

    public void initVertexData(float scale,float a, float b) {

		a*=scale;
		b*=scale;
		float xOffset=a/2;
		float yOffset=b/2;
        shaderBuffer.vCount=6;
		float[] vertices=new float[]{
			-xOffset,-yOffset,0,
			xOffset,yOffset,0,
			-xOffset,yOffset,0,
        	
        	-xOffset,-yOffset,0,
        	xOffset,-yOffset,0,
        	xOffset,yOffset,0
		};
        shaderBuffer.setVertexBuffer(vertices);
		float[] normals=new float[]{
				0,0,1,
				0,0,1,
				0,0,1,
				
				0,0,1,
				0,0,1,
				0,0,1,
		};
        shaderBuffer.setNormalBuffer(normals);

        float[] textures=new float[]{
        		0,1,
        		1,0,
        		0,0,
        		
        		0,1,
        		1,1,
        		1,0,
        };
        shaderBuffer.setTexcoorBuffer(textures);
	}

	public void initShader(MySurfaceView mv)
    {

        mVertexShader=ShaderUtil.loadFromAssetsFile("simple_13_02/vertex_tex_light.sh", mv.getResources());

        mFragmentShader=ShaderUtil.loadFromAssetsFile("simple_13_02/frag_tex_light.sh", mv.getResources());

        shaderHandle = new ShaderHandle(ShaderUtil.createProgram(mVertexShader, mFragmentShader) , false);
        shaderHandle.setM_A_TexCoorsHandleName("aTexCoor").build();
        
    }
	public void drawSelf(int texId)
    {
        shaderHandle.program()
                .bindMMaxtrixHandle()
                .bindMVPMaxtrixHandle()
                .bindLightHandle()
                .bindCameraHandle()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindNormalHandle(shaderBuffer.getNormalBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindDefaultTextureHandle(texId)
                .run(GLES30.GL_TRIANGLES, shaderBuffer.vCount);
    }
}
